package code.c09;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/11/1 15:36
 * @package code.e09
 * @description 创建线程的方式三：实现Callable接口 --- jdk 5.0 新增
 * 例子：遍历 100 以内的偶数，并返回总和
 */

public class Ex07 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        NumThread numThread = new NumThread();
        FutureTask futureTask = new FutureTask(numThread);
        Thread thread = new Thread(futureTask);

        thread.start();
        Object sum = futureTask.get();
        System.out.println(sum);
    }
}

class NumThread implements Callable {

    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 0; i < 100; i++) {
            System.out.println(i);
            if (i % 2 == 0) {
                System.out.println(i);
                sum += i;
            }
        }

        return sum;
    }
}
